package com.bobochang.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bobochang.warehouse.annotation.BusLog;
import com.bobochang.warehouse.constants.WarehouseConstants;
import com.bobochang.warehouse.entity.CurrentUser;
import com.bobochang.warehouse.entity.LocationConstruction;
import com.bobochang.warehouse.entity.Result;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.LocationConstructionService;
import com.bobochang.warehouse.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/location")
@RestController
@Transactional
@BusLog(name = "施工位置管理")
@Slf4j
public class LocationController {

    @Resource
    private LocationConstructionService locationService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询所有施工位置
     *
     * @return
     */
    @GetMapping("/location-list")
    public Result locationList(LocationConstruction locationConstruction) {
        LambdaQueryWrapper<LocationConstruction> queryWrapper = new LambdaQueryWrapper<LocationConstruction>()
                .eq(locationConstruction.getContractId() != null, LocationConstruction::getContractId, locationConstruction.getContractId())
                .like(locationConstruction.getLocationName() != null, LocationConstruction::getLocationName, locationConstruction.getLocationName());
        return Result.ok(locationService.list(queryWrapper));
    }

    /**
     * 根据项目id查位置
     */
    @GetMapping("/location-list/{id}")
    public Result locationListById(@PathVariable Integer id) {
        LambdaQueryWrapper<LocationConstruction> queryWrapper = new LambdaQueryWrapper<LocationConstruction>()
                .eq(id != null, LocationConstruction::getContractId, id);
        return Result.ok(locationService.list(queryWrapper));
    }

    /**
     * 分页查询施工位置
     *
     * @param page
     * @param locationConstruction
     * @return
     */
    @RequestMapping("/location-page-list")
    public Result locationPageList(Page page, LocationConstruction locationConstruction) {
        // 分页查询施工位置
        Page resultPage = locationService.queryLocationPage(page, locationConstruction);
        return Result.ok(resultPage);
    }

    /**
     * 添加施工位置
     */
    @RequestMapping("/location-add")
    @BusLog(descrip = "添加施工位置")
    public Result addLocation(@RequestBody LocationConstruction locationConstruction,
                              @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        // 添加施工位置
        boolean isSave = locationService.save(locationConstruction);
        if (isSave) {
            return Result.ok("添加成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "添加失败");
        }
    }

    /**
     * 删除施工位置
     * @param locationId
     * @return
     */
    @RequestMapping("/location-delete/{locationId}")
    @BusLog(descrip = "删除施工位置")
    public Result deleteLocation(@PathVariable Integer locationId) {
        boolean isDelete = locationService.removeById(locationId);
        if (isDelete) {
            return Result.ok("删除成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "删除失败");
        }
    }

    /**
     * 修改施工位置
     * @param locationConstruction
     * @param token
     * @return
     */
    @RequestMapping("/location-update")
    @BusLog(descrip = "修改施工位置")
    public Result updateLocation(@RequestBody LocationConstruction locationConstruction,
                                 @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        // 修改施工位置
        boolean isUpdate = locationService.updateById(locationConstruction);
        if (isUpdate) {
            return Result.ok("修改成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
        }
    }



}
